<?php

namespace app\service;

use app\model\PlanModel;
use app\model\TaskModel;
use app\tool\SingletonTrait;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class NeedProjectService
{
    use SingletonTrait;

    /**
     * 查询月度剧集环节计划的剧集列表
     * @param int $yearMonth
     * @param int $projectId
     * @param $categoryIdList
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getYearMonthEpisodeStepCategoryPlan(int $yearMonth, int $projectId, $categoryIdList): array
    {
        $getMonthTime = get_month_time($yearMonth);
        $entityCode = [];
        $yearStartTime = $getMonthTime['sdate'];
        $yearEndTime = $getMonthTime['edate'];
        $list = PlanModel::field('entity.code')
            ->join('task_relation_tag', 'plan.task_id = task_relation_tag.task_id')
            ->join('entity', 'entity.id = plan.entity_id')
            ->where('plan.project_id', $projectId)
            ->where('plan.type', TaskModel::STEP_TYPE_STEP_CATEGORY)
            ->whereBetween("plan.end_time", [$yearStartTime, $yearEndTime])
            ->whereRaw("task_relation_tag.link_module_code in ('step_category','show_step_category')")
            ->whereIn(' task_relation_tag.link_id', $categoryIdList)
            ->group("entity.code")
            ->select();
        if (empty($list->toArray())) {
            return [];
        }
        foreach ($list->toArray() as $code) {
            $entityCode[] = $code;
        }
        return $entityCode;
    }

}